package org.ala.distributed_transaction_commons.configure;

import org.ala.distributed_transaction_commons.communication.Coordinator;
import org.ala.distributed_transaction_commons.communication.Participant;
import org.ala.distributed_transaction_commons.redis.DistributedTransactionRedisTools;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.core.env.Environment;


/**
 * 分布式事务相关配置
 * 
 * @author 骆毅(ala)
 * @date 2024年2月17日
 */
@Configuration
public class DistributedTransactionConfiguration {

	
	/**
	 * 配置文件解析
	 */
	@Bean(value="distributedTransactionConfigure")
	@Lazy
	public DistributedTransactionConfigure distributedTransactionConfigure(Environment environment) {
		DistributedTransactionConfigure c = new DistributedTransactionConfigure();
		c.setEnvironment(environment);
		return c;
	}
	
	
	/**
	 * redis工具
	 * @throws Exception 
	 */
	@Bean("distributedTransactionRedisTools")
	@Lazy
	public DistributedTransactionRedisTools distributedTransactionRedisTools(
			@Qualifier("distributedTransactionConfigure") DistributedTransactionConfigure distributedTransactionConfigure) throws Exception {
		DistributedTransactionRedisTools tools = new DistributedTransactionRedisTools();
		tools.setConf(distributedTransactionConfigure.getRedisConfigure());
		return tools;
	}
	
	
	/**
	 * 协调者
	 */
	@Bean("coordinator")
	@Lazy
	public Coordinator coordinator(
			@Qualifier("distributedTransactionRedisTools") DistributedTransactionRedisTools distributedTransactionRedisTools) {
		Coordinator c = new Coordinator();
		c.setDistributedTransactionRedisTools(distributedTransactionRedisTools);
		return c;
	}
	/**
	 * 参与者
	 */
	@Bean("participant")
	@Lazy
	public Participant participant(
			@Qualifier("distributedTransactionRedisTools") DistributedTransactionRedisTools distributedTransactionRedisTools) {
		Participant p = new Participant();
		p.setDistributedTransactionRedisTools(distributedTransactionRedisTools);
		return p;
	}
	
	
//	/**
//	 * 分布式事务生成工厂
//	 */
//	@Bean
//	@Lazy
//	public AbstractDistributedTransactionFactory distributedTransactionFactory(ApplicationContext context) {
//		AbstractDistributedTransactionFactory f = new AbstractDistributedTransactionFactory();
//		f.setApplicationContext(context);
//		return f;
//	}
}
